/*
 * Copyright (c) 2014. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
 * http://www.apache.org/licenses/LICENSE-2.0
 */

package com.dc.gameserver.serverCore.controller.handleController.itemsController;

import com.dc.gameserver.exceptions.dataException;
import com.dc.gameserver.proto.gameserverprotoVo.buyLevelNumRequest;
import com.dc.gameserver.proto.gameserverprotoVo.buyLevelNumResponse;
import com.dc.gameserver.serverCore.controller.AbstractController.AbstractController;
import com.dc.gameserver.serverCore.controller.AbstractController.IController;
import com.dc.gameserver.serverCore.model.dbsql.modelCache.ModelCacheService;
import com.dc.gameserver.serverCore.model.dbsql.modelService.PlayerService;
import com.dc.gameserver.serverCore.model.dbsql.modelVo.TPointPrograss;
import com.dc.gameserver.serverCore.model.dbsql.modelVo.TRole;
import com.dc.gameserver.serverCore.model.gamemodel.LevelModel;
import com.dc.gameserver.serverCore.model.gamemodel.script.ScriptFactory;
import com.dc.gameserver.serverCore.service.character.PlayerInstance;
import com.google.protobuf.MessageLite;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 石头哥哥 </br>
 *         dcserver1.3 </br>
 *         Date:13-12-27 </br>
 *         Time:下午2:34 </br>
 *         Package:{@link com.dc.gameserver.serverCore.controller.handleController.itemsController}</br>
 *         Comment：购买可玩关卡
 */
@Service
public class buyLevelController extends AbstractController {

    @Resource
    private ModelCacheService modelCacheService;

    @Resource
    private PlayerService playerService;

    @Override
    public void PostConstruct() {
        int ID= buyLevelNumRequest.msgID.ID_VALUE;
        GAME_CONTROLLERS[ID]=this;
        IController.MESSAGE_LITE[ID]=buyLevelNumRequest.getDefaultInstance();
    }

    /**
     * messageLite数据结构体分发
     *
     * @param messageLite 数据载体
     * @param player      active object
     * @throws Exception 注意messageLite应该递交给GC直接处理  ，set null
     */
    @Override
    @SuppressWarnings("unchecked")
    public void DispatchMessageLit(MessageLite messageLite, PlayerInstance player) throws Exception {
        buyLevelNumRequest levelNumRequest= (buyLevelNumRequest) messageLite;
        int levelID=levelNumRequest.getLevelID();
        levelNumRequest=null;
        int roleID= player.getID();
        String role_prefix=ModelCacheService.ROLE_PREFIX(roleID);
        TRole tRole= (TRole) modelCacheService.findByPrefixID(role_prefix);
        LevelModel levelModel= ScriptFactory.pointModelFastMap.get(levelID);
        int castCoin=levelModel.getJadeCoinCost();
        int tempCoin=tRole.getRolexianyunum();
        buyLevelNumResponse.Builder builder=buyLevelNumResponse.newBuilder();
        if (tempCoin>=castCoin){
            try {
                tRole.setRolexianyunum(tempCoin-castCoin);
                playerService.updateRole(tRole);
                modelCacheService.addObjectPrefixID(role_prefix,tRole);
                builder.setJadeCoin(tRole.getRolexianyunum());
                builder.setBuyStatus(0x1);
                //解锁关卡
                String prograss_prefix=ModelCacheService.POINT_PROGRASS_PREFIX(roleID);
                List<TPointPrograss> prograssList= (List<TPointPrograss>) modelCacheService.findListByPrefixID(prograss_prefix);
                TPointPrograss  _prograss=null;
                for (TPointPrograss prograss:prograssList){
                     if (prograss.getPointid()==levelID){
                         _prograss=prograss;
                         prograssList.remove(prograss);
                         break;
                     }
                }
                if (_prograss!=null){
                    _prograss.setPointpercompletenum(0x0);
                    playerService.updateLevel(_prograss);
                    prograssList.add(_prograss);
                    modelCacheService.addListByPrefixID(prograss_prefix,prograssList);
                }else {
                    throw new dataException("_prograss is null");
                }
            } catch (dataException e){
                builder.setBuyStatus(0x2);
            }
        }else {
            builder.setBuyStatus(0x2);
        }
        builder.setLevelID(levelID);
        player.DirectSend(wrappedBufferShort(buyLevelNumResponse.msgID.ID_VALUE,builder.build()));
    }
}
